package com.aaa.marketresearch.android.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import com.aaa.marketresearch.android.data.UserInfo;
import com.aaa.marketresearch.android.data.domain.Market;
import com.aaa.marketresearch.android.service.HttpClient;
import com.aaa.marketresearch.android.util.SimpleLog;
import com.aaa.marketresearch.android.util.connector.IDataTextResponse;
import com.aaa.marketresearch.android.widget.MarketAdapter;
import com.aaa.marketresearch.android.widget.NavigationMenu;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Nail
 */
public class MarketScreen extends Activity {

    private ListView listView;

    private List<Market> markets = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_market_screen);

        this.listView = (ListView) findViewById(R.id.marketList);
    }

    @Override
    protected void onResume() {
        super.onResume();

         HttpClient httpClient = new HttpClient(this.getApplicationContext());
        httpClient.getMarkets(new IDataTextResponse() {
            @Override
            public void onTextResponse(String text) {
                MarketScreen.this.marketsLoaded(text);
            }

            @Override
            public void onError() {
                SimpleLog.info("addMarketButton onError");
            }
        });

        NavigationMenu navigationMenu = (NavigationMenu) findViewById(R.id.navigationMenu);
        navigationMenu.reload();
        navigationMenu.hideMarket();
    }

    public void familyMemberPressed(View view) {
        Intent intent = new Intent(this, FamilyScreen.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        this.startActivity(intent);
    }

    public void marketPressed(View view) {
        Intent intent = new Intent(this, MarketScreen.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        this.startActivity(intent);
    }

    public void offerPressed(View view) {
        Intent intent = new Intent(this, OfferScreen.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        this.startActivity(intent);
    }

    public void billPressed(View view) {
        Intent intent = new Intent(this, BillScreen.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        this.startActivity(intent);
    }

    public void addMarketButton(View view) {
        SimpleLog.info("addMarketButton");

        UserInfo userInfo = UserInfo.getInstance();
        Market market = new Market();
        userInfo.setCurrentMarket(market);

        Intent intent = new Intent(this, MarketEditScreen.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        this.startActivity(intent);
    }

    private void marketsLoaded(String json) {
        if (json != null) {
            JSONParser parser = new JSONParser();
            JSONArray data = null;
            try {
                data = (JSONArray) parser.parse(json);
            } catch (ParseException e) {
                SimpleLog.error(e.toString());
            }
            Object obj = null;

            int n = data.size();
            if (n > 0) {
                this.markets = new ArrayList<>(n);
                for (int i = 0; i < n; i++) {
                    JSONObject jObj = (JSONObject) data.get(i);
                    Market market = new Market();
                    market.setWithJSONObject(jObj);
                    this.markets.add(market);
                }

                MarketAdapter adapter = new MarketAdapter(this, this.markets);

                this.listView.setAdapter(adapter);
            }
        }
    }
}